<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="30%" :title="`${drawerProps.title}题库`">
    <el-form
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.title === '查看'"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="章节名称" prop="name">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写章节名称" clearable autofocus></el-input>
      </el-form-item>
      <el-form-item label="章节简介" prop="introduce">
        <el-input v-model="drawerProps.row!.introduce" placeholder="请填写章节简介" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="handleSubmit"> 提交 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="editChapterDrawer">
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<any>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  course_id?: string;
  parent_id?: string;
  order_number?: string;
}

const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// const content_course_id = ref<string>("");
// const content_chapter_id = ref<string>("");
// const content_name = ref<string>("");
// const content_introduce = ref<string>("");
const drawerVisible = ref(false);

const rules = reactive({
  name: [{ required: true, message: "请填写题库名称" }]
});

// 接收父组件传过来的参数
const acceptParams = (params: any) => {
  drawerProps.value = params;
  drawerVisible.value = true;

  // content_course_id.value = drawerProps.value.course_id;
  // content_chapter_id.value = params.row.chapter_id ?? "";
  // content_name.value = params.row.name ?? "";
  // content_introduce.value = params.row.introduce ?? "";
};

// 提交数据（新增/编辑）
const handleSubmit = async () => {
  try {
    const chapterInfo = reactive<any>({
      chapter_id: drawerProps.value.row.chapter_id,
      name: drawerProps.value.row.name,
      introduce: drawerProps.value.row.introduce,
      course_id: drawerProps.value.course_id,
      parent_id: drawerProps.value.parent_id,
      order_number: drawerProps.value.order_number
    });
    const data = await drawerProps.value.api!({ ...chapterInfo });
    if (data.code !== "") {
      ElMessage.error(data.msg);
      return;
    }
    ElMessage.success({ message: `提交成功！` });
    drawerProps.value.getTableList!();
    drawerVisible.value = false;
  } catch (error) {
    console.log(error);
  }
  return true;
};

defineExpose({
  acceptParams
});
</script>
<style scoped lang="scss">
@import "chapterEditor";
</style>
